package com.wake.mycomposebook

import android.content.res.Configuration.ORIENTATION_PORTRAIT
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.Crossfade
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.PlayArrow
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.Surface
import androidx.compose.runtime.*
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.sp
import com.guru.fontawesomecomposelib.FaIcon
import com.guru.fontawesomecomposelib.FaIcons
import com.wake.mycomposebook.entry.enums.BottomNavType
import com.wake.mycomposebook.ui.home.PalletMenu
import com.wake.mycomposebook.ui.home.myhome.StudyHomeScreen
import com.wake.mycomposebook.ui.sys.AppThemeState
import com.wake.mycomposebook.ui.sys.SysUiController
import com.wake.mycomposebook.utils.RotateIcon
import com.wake.mycomposebook.utils.TestTags
import com.wake.theme.modifier.*
import kotlinx.coroutines.launch

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            val sysUiController = remember { SysUiController(window) }
            val appThemeState = remember {
                mutableStateOf(AppThemeState())
            }
            BaseView(appThemeState.value, sysUiController) {
                MainAppContent(appThemeState)
            }

        }
    }
}

@Composable
fun BaseView(
    appThemeState: AppThemeState,
    sysUiController: SysUiController?,
    content: @Composable () -> Unit
) {
    val color = when (appThemeState.pallet) {
        ColorPallet.GREEN -> green700
        ColorPallet.BLUE -> blue700
        ColorPallet.ORANGE -> orange700
        ColorPallet.PURPLE -> purple700
        else -> green700
    }
    MyComposeBookMaterial3Theme(darkTheme = appThemeState.darkTheme, colorPallet = appThemeState.pallet) {
        sysUiController?.setStatusBarColor(color = MaterialTheme.colorScheme.onPrimaryContainer, darkIcons = appThemeState.darkTheme)
        content()
    }
}

@OptIn(ExperimentalMaterialApi::class)
@Composable
fun HomeScreenContent(
    homeScreen: BottomNavType,
    appThemeState: MutableState<AppThemeState>,
    chooseColorBottomModalState: ModalBottomSheetState,
    modifier: Modifier
) {
    Column(modifier = modifier) {
        Crossfade(targetState = homeScreen) { screen ->
            Surface(color = MaterialTheme.colorScheme.background) {
                when (screen) {
                    BottomNavType.HOME -> {
//                        Text(text = "Home")
//                        DemoHomeScreen(appThemeState = appThemeState, chooseColorBottomModalState = chooseColorBottomModalState)
                        StudyHomeScreen(appThemeState = appThemeState,   chooseColorBottomModalState)
                    }
                    BottomNavType.WIDGETS -> {
                        Text(text = "Widgets")
                    }
                    BottomNavType.ANIM -> {
                        Text(text = "Anim")
                    }
                    BottomNavType.DEMOUI -> {
                        Text(text = "DemoUi")
                    }
                    BottomNavType.TEMPLATE -> {
                        Text(text = "TEMPLATE")
                    }
                }
            }
        }
    }
}

@OptIn(ExperimentalMaterialApi::class)
@Composable
fun MainAppContent(appThemeState: MutableState<AppThemeState>) {
    val homeScreen = rememberSaveable {
        mutableStateOf(BottomNavType.HOME)
    }
    val bottomNavBarContentDes = stringResource(id = R.string.a11y_bottom_navigation_bar)
    val chooseColorBottomModalState = rememberModalBottomSheetState(initialValue = ModalBottomSheetValue.Hidden)

    val coroutineScope = rememberCoroutineScope()

    ModalBottomSheetLayout(sheetState = chooseColorBottomModalState, sheetContent = {
        PalletMenu(modifier = Modifier.align(Alignment.CenterHorizontally)) { newPalletSelected ->
            appThemeState.value = appThemeState.value.copy(pallet = newPalletSelected)
            coroutineScope.launch {
                chooseColorBottomModalState.hide()
            }
        }
    }) {
        val config = LocalConfiguration.current
        val orientation = config.orientation

        when (orientation) {
            ORIENTATION_PORTRAIT -> {
                Column {
                    HomeScreenContent(
                        homeScreen = homeScreen.value,
                        appThemeState = appThemeState,
                        chooseColorBottomModalState = chooseColorBottomModalState,
                        modifier = Modifier.weight(1f)
                    )
                    BottomNavigationContent(
                        modifier = Modifier
                            .semantics {
                                contentDescription = bottomNavBarContentDes
                            }
                            .testTag(TestTags.BOTTOM_NAV_TEST_TAG),
                        homeScreenState = homeScreen
                    )
                }
            }
            else -> {
                Row(modifier = Modifier.fillMaxSize()) {
                    HomeScreenContent(
                        homeScreen = homeScreen.value,
                        appThemeState = appThemeState,
                        chooseColorBottomModalState = chooseColorBottomModalState,
                        modifier = Modifier.weight(1f)
                    )

                    NavigationRailContent(modifier = Modifier
                        .semantics {
                            contentDescription = bottomNavBarContentDes
                        }
                        .testTag(TestTags.BOTTOM_NAV_TEST_TAG),
                        homeScreenState = homeScreen
                    )

                }
            }
        }
    }

}

@Composable
fun BottomNavigationContent(
    modifier: Modifier = Modifier,
    homeScreenState: MutableState<BottomNavType>
) {
    var animate by remember { mutableStateOf(false) }
    NavigationBar(
        modifier = modifier,
    ) {
        NavigationBarItem(
            icon = {
                FaIcon(
                    faIcon = FaIcons.Home,
                    tint = androidx.compose.material3.LocalContentColor.current.copy(alpha = LocalContentAlpha.current)
                )
            },
            selected = homeScreenState.value == BottomNavType.HOME,
            onClick = {
                homeScreenState.value = BottomNavType.HOME
                animate = false
            },
            label = {
                androidx.compose.material3.Text(
                    text = stringResource(id = R.string.navigation_item_home),
                    style = TextStyle(fontSize = 12.sp)
                )
            },
            modifier = Modifier.testTag(TestTags.BOTTOM_NAV_HOME_TEST_TAG)
        )
        NavigationBarItem(
            icon = {
                FaIcon(
                    faIcon = FaIcons.Tools, tint = androidx.compose.material3.LocalContentColor
                        .current.copy(
                            alpha =
                            LocalContentAlpha.current
                        )
                )
            },
            selected = homeScreenState.value == BottomNavType.WIDGETS,
            onClick = {
                homeScreenState.value = BottomNavType.WIDGETS
                animate = false
            },
            label = {
                androidx.compose.material3.Text(
                    text = stringResource(id = R.string.navigation_item_widgets),
                    style = TextStyle(fontSize = 12.sp)
                )
            },
            modifier = Modifier.testTag(TestTags.BOTTOM_NAV_WIDGETS_TEST_TAG)
        )
        NavigationBarItem(
            icon = {
                RotateIcon(
                    state = animate,
                    asset = Icons.Default.PlayArrow,
                    angle = 720f,
                    duration = 2000
                )
            },
            selected = homeScreenState.value == BottomNavType.ANIM,
            onClick = {
                homeScreenState.value = BottomNavType.ANIM
                animate = true
            },
            label = {
                androidx.compose.material3.Text(
                    text = stringResource(id = R.string.navigation_item_animation),
                    style = TextStyle(fontSize = 12.sp)
                )
            },
            modifier = Modifier.testTag(TestTags.BOTTOM_NAV_ANIM_TEST_TAG)

        )
        NavigationBarItem(
            icon = {
                FaIcon(
                    faIcon = FaIcons.LaptopCode, tint = androidx.compose.material3.LocalContentColor.current.copy(
                        alpha =
                        LocalContentAlpha.current
                    )
                )
            },
            selected = homeScreenState.value == BottomNavType.DEMOUI,
            onClick = {
                homeScreenState.value = BottomNavType.DEMOUI
                animate = false
            },
            label = {
                androidx.compose.material3.Text(
                    text = stringResource(id = R.string.navigation_item_demoui),
                    style = TextStyle(fontSize = 12.sp)
                )
            },
            modifier = Modifier.testTag(TestTags.BOTTOM_NAV_DEMO_UI_TEST_TAG)
        )
        NavigationBarItem(
            icon = {
                FaIcon(
                    faIcon = FaIcons.LayerGroup, tint = androidx.compose.material3.LocalContentColor
                        .current.copy(
                            alpha =
                            LocalContentAlpha.current
                        )
                )
            },
            selected = homeScreenState.value == BottomNavType.TEMPLATE,
            onClick = {
                homeScreenState.value = BottomNavType.TEMPLATE
                animate = false
            },
            label = {
                androidx.compose.material3.Text(
                    text = stringResource(id = R.string.navigation_item_profile),
                    style = TextStyle(fontSize = 12.sp)
                )
            },
            modifier = Modifier.testTag(TestTags.BOTTOM_NAV_TEMPLATE_TEST_TAG)

        )
    }
}

@Composable
private fun NavigationRailContent(
    modifier: Modifier,
    homeScreenState: MutableState<BottomNavType>,
) {
    var animate by remember { mutableStateOf(false) }
    androidx.compose.material3.NavigationRail(
        modifier = modifier,
    ) {
        androidx.compose.material3.NavigationRailItem(
            icon = {
                FaIcon(
                    faIcon = FaIcons.Home,
                    tint = androidx.compose.material3.LocalContentColor.current.copy(alpha = LocalContentAlpha.current)
                )
            },
            selected = homeScreenState.value == BottomNavType.HOME,
            onClick = {
                homeScreenState.value = BottomNavType.HOME
                animate = false
            },
            label = {
                androidx.compose.material3.Text(
                    text = stringResource(id = R.string.navigation_item_home),
                    style = TextStyle(fontSize = 12.sp)
                )
            },
            modifier = Modifier.testTag(TestTags.BOTTOM_NAV_HOME_TEST_TAG)
        )
        androidx.compose.material3.NavigationRailItem(
            icon = {
                FaIcon(
                    faIcon = FaIcons.Tools, tint = androidx.compose.material3.LocalContentColor
                        .current.copy(
                            alpha =
                            LocalContentAlpha.current
                        )
                )
            },
            selected = homeScreenState.value == BottomNavType.WIDGETS,
            onClick = {
                homeScreenState.value = BottomNavType.WIDGETS
                animate = false
            },
            label = {
                androidx.compose.material3.Text(
                    text = stringResource(id = R.string.navigation_item_widgets),
                    style = TextStyle(fontSize = 12.sp)
                )
            },
            modifier = Modifier.testTag(TestTags.BOTTOM_NAV_WIDGETS_TEST_TAG)
        )
        androidx.compose.material3.NavigationRailItem(
            icon = {
                RotateIcon(
                    state = animate,
                    asset = Icons.Default.PlayArrow,
                    angle = 720f,
                    duration = 2000
                )
            },
            selected = homeScreenState.value == BottomNavType.ANIM,
            onClick = {
                homeScreenState.value = BottomNavType.ANIM
                animate = true
            },
            label = {
                androidx.compose.material3.Text(
                    text = stringResource(id = R.string.navigation_item_animation),
                    style = TextStyle(fontSize = 12.sp)
                )
            },
            modifier = Modifier.testTag(TestTags.BOTTOM_NAV_ANIM_TEST_TAG)

        )
        androidx.compose.material3.NavigationRailItem(
            icon = {
                FaIcon(
                    faIcon = FaIcons.LaptopCode, tint = androidx.compose.material3.LocalContentColor.current.copy(
                        alpha =
                        LocalContentAlpha.current
                    )
                )
            },
            selected = homeScreenState.value == BottomNavType.DEMOUI,
            onClick = {
                homeScreenState.value = BottomNavType.DEMOUI
                animate = false
            },
            label = {
                androidx.compose.material3.Text(
                    text = stringResource(id = R.string.navigation_item_demoui),
                    style = TextStyle(fontSize = 12.sp)
                )
            },
            modifier = Modifier.testTag(TestTags.BOTTOM_NAV_DEMO_UI_TEST_TAG)
        )
        androidx.compose.material3.NavigationRailItem(
            icon = {
                FaIcon(
                    faIcon = FaIcons.LayerGroup, tint = androidx.compose.material3.LocalContentColor
                        .current.copy(
                            alpha =
                            LocalContentAlpha.current
                        )
                )
            },
            selected = homeScreenState.value == BottomNavType.TEMPLATE,
            onClick = {
                homeScreenState.value = BottomNavType.TEMPLATE
                animate = false
            },
            label = {
                androidx.compose.material3.Text(
                    text = stringResource(id = R.string.navigation_item_profile),
                    style = TextStyle(fontSize = 12.sp)
                )
            },
            modifier = Modifier.testTag(TestTags.BOTTOM_NAV_TEMPLATE_TEST_TAG)

        )
    }
}

@OptIn(
    ExperimentalAnimationApi::class,
    ExperimentalFoundationApi::class,
    ExperimentalMaterialApi::class
)
@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    val appThemeState = remember { mutableStateOf(AppThemeState(false, ColorPallet.GREEN)) }
    BaseView(appThemeState.value, null) {
        MainAppContent(appThemeState)
    }
}